﻿using System.Windows.Controls;
using Shrestha.Database;
using Shrestha.Trigraph;
using System.Windows;

namespace Shrestha.DataWizard
{
    /// <summary>
    /// Interaction logic for SelectRows.xaml
    /// </summary>
    public partial class SelectRows : Page
    {
        private DataSourceLoader dataSourceLoader;
        private DbManager dbMan;

        public SelectRows(DbManager dbMan, DataSourceLoader ds)
        {
            this.dbMan = dbMan;
            this.dataSourceLoader = ds;
            InitializeComponent();
            TriGraphResources.rowNames = dbMan.GetRows();
            comboBoxAdditional.ItemsSource = comboBoxDate.ItemsSource = comboBoxLatitude.ItemsSource = comboBoxLongitude.ItemsSource = TriGraphResources.rowNames;
        }

        private void buttonNext_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (validated())
            {
                TriGraphResources.latName = " " + comboBoxLatitude.SelectedValue.ToString() + " ";
                TriGraphResources.lngName = " " + comboBoxLongitude.SelectedValue.ToString() + " ";
                TriGraphResources.timeName = " " + comboBoxDate.SelectedValue.ToString() + " ";
                TriGraphResources.groupName = comboBoxAdditional.SelectedValue == null ? null : " " + comboBoxAdditional.SelectedValue.ToString() + " ";
                if (comboBoxAdditional.SelectedValue != null)
                {
                    TriGraphResources.groupList = dbMan.GetGroupingEvent();
                    if (TriGraphResources.groupList == null)
                    {
                        MessageBox.Show("The grouping column is really large. Please select another row for grouping");
                    }
                }
                dataSourceLoader.DataLoaded = true;
                dataSourceLoader.Close();
            }
            else
            {
                MessageBox.Show("Please select proper rows for Latitude, Longitude and Date.");
                comboBoxLatitude.Focus();
            }
        }

        private bool validated()
        {
            return !comboBoxLatitude.SelectedValue.ToString().Equals("") && !comboBoxLongitude.SelectedValue.ToString().Equals("") && !comboBoxDate.SelectedValue.ToString().Equals("");
        }

        private void buttonCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            dataSourceLoader.DataLoaded = false;
            dataSourceLoader.Close();
        }
    }
}
